/****************************************************************************

	COPYRIGHT(C) MAXSI SOFTWARE, JONAS 'SORTIE' TERMANSEN 2008, 2009, 2010

    This file is part of Maxsi Engine.

    Maxsi Engine is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Maxsi Engine is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Maxsi Engine. If not, see <http://www.gnu.org/licenses/>.

	*/ #include "MaxsiEngineContributors.h" /*

	If you modify this file, please enter your name below and provide contact
	information in MaxsiEngineContributors.h. For more information please see
	MaxsiEngineContributors.h.
	
	Contributors to this file:

	- Jonas 'Sortie' Termansen
	- [your name here]

	MaxsiEngine.dll
	A linkable version of Maxsi Engine

	MaxsiEngineFile.h
	Libary for import and export of Maxsi Engine Files (.mef)

****************************************************************************/

#ifndef MaxsiEngineFile_h
#define MaxsiEngineFile_h

namespace MaxsiEngine
{

	enum MaxsiEngineFileHandle
	{
		INFO_EXPORT_SUCCESS = 2,				// The data was successfully imported into memory
		INFO_IMPORT_SUCCESS = 1,				// The data was successfully imported into memory
		ERROR_NOT_IMPLEMENTED = 0,				// The requested feature is not implemented yet

		ERROR_COULDNT_OPEN_FILE = -1,			// The specified file cannot be opened for binary reading
		ERROR_IMPORT_INVALID_HEADER = -2,		// The datastream doesn't start with the "ME" + trailing zero sequence
		ERROR_IMPORT_INVALID_VERSION = -3,		// The specified version is not supported
		ERROR_IMPORT_EXPIRED_VERSION = -4,		// The specified version has been supported but isn't anymore

		ERROR_INVALID_CALL_NO_FILENAME = -5,	// The called function requires a valid filename

		ERROR_EXPORT_INVALID_VERSION = -6,		// The specified version is not supported
		ERROR_EXPORT_INVALID_BUFFER_SIZE = -7	// Allocated more or less memory than required
	};

	CLASSLINK MaxsiEngineFileFragment
	{
	public:
		MaxsiEngineFileFragment		( void );
		~MaxsiEngineFileFragment	( void );

	private:
		char*	FileName;
		__int64	FileSize;
		char*	FileContent;

	public:
		MaxsiEngineFileFragment*	Fragment;
		List						Variables;

		char*	Read(__int64* BufferSize = 0);
		int		Write(char* Data, char** CallerPos = 0);
		int		Write(char* Data, __int64 DataLen, const char* Path = 0, char** CallerPos = 0);
		char*	GetFileName( void );
		__int64	GetFileSize( void );

	};

	typedef MEVariable MEFFVariable;

	LINK	MEFFVariable*	GetVariable(List list, char* VariableName);
	LINK	MEFFVariable*	SetVariable(List list, char* VariableName, char* Buffer, unsigned int Buffer_Size);
	LINK	char*			GetVariableData(List list, char* VariableName);
	LINK	List			DuplicateVariableList(List list);
	LINK	bool			DeleteVariableList(List list);

	CLASSLINK MaxsiEngineFile
	{
	public:
		MaxsiEngineFile		( void );
		MaxsiEngineFile		( char* Path );
		~MaxsiEngineFile	( void );

		unsigned int				Version;
		unsigned __int64			NumFiles;
		unsigned __int64			*FilePos;
		List						Variables;
		MaxsiEngineFileFragment*	Fragment;

		// Importing data from a .MEF formatted file/buffer
		int	ImportFile(const char* Path, ProgressFunc_Type ProgressFunc = 0);
		int	ImportFile(char* Data, __int64 DataLen, ProgressFunc_Type ProgressFunc = 0);
		int	ImportFile_Version_1(char* Data, __int64 DataLen, ProgressFunc_Type ProgressFunc = 0);
		int	ImportFile_Version_2(char* Data, __int64 DataLen, ProgressFunc_Type ProgressFunc = 0);

		// Importing data to file/buffer written in the .MEF format
		int	ExportFile(const char* Path, ProgressFunc_Type ProgressFunc = 0);
		int	ExportFile(char** Data, unsigned __int64* DataLen, ProgressFunc_Type ProgressFunc = 0);
		int	ExportFile_Version_1(char** Data, unsigned __int64* DataLen, ProgressFunc_Type ProgressFunc = 0);
		int	ExportFile_Version_2(char** Data, unsigned __int64* DataLen, ProgressFunc_Type ProgressFunc = 0);

		// Extract the content(s) to a folder
		int	ExtractFile(char** Buffer, unsigned __int64* DataLen, MaxsiEngineFileFragment* TheFragment);
		int	ExtractFile(const char* FileName, const char* Directory);
		int	ExtractFiles(const char* Directory);
		int	ExtractFile(MaxsiEngineFileFragment* Fragment, const char* Directory);

		// Creates a new fragment and forwards the call
		MaxsiEngineFileFragment*	ReadFile(const char* Path, List* Variables = 0);
		MaxsiEngineFileFragment*	WriteFile(char* Data);
		MaxsiEngineFileFragment*	WriteFile(char* Path, char* Alias, List Variables = 0);
		MaxsiEngineFileFragment*	WriteFile(const char* Path, char* Data, long DataLen, List Variables = 0);
	};

	// Incase anyone is lazy
	typedef MaxsiEngineFile MEF;

};

#endif
